﻿<%@ WebHandler Language="C#" Class="AdminHandlerService" %>

using System;
using System.Web;
using System.IO;

using System.Collections;
using System.Collections.Generic;
using System.Reflection;

using XJX.BusinessLayer;

using XJX.Page;

public class AdminHandlerService : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";

        string response = "";

        String methodName = context.Request["method"];
        Type type = this.GetType();
        MethodInfo method = type.GetMethod(methodName);

        if (method == null)
        {
            response = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "调用的方法不存在！").ToJson(); return;
        }

        try
        {
            BeforeInvoke(methodName);
            response = method.Invoke(this, null).ToString();
        }
        catch (Exception ex)
        {
            Hashtable result = new Hashtable();
            result["error"] = -1;
            result["message"] = ex.Message;
            result["stackTrace"] = ex.StackTrace;
            String json = XJX.BusinessLayer.JSON.Encode(result);
            context.Response.Clear();
            context.Response.Write(json);
        }
        finally
        {
            AfterInvoke(methodName);
            HttpContext.Current.Response.Write(response);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    //权限管理
    protected void BeforeInvoke(String methodName)
    {
        //Hashtable user = GetUser();
        //if (user.role == "admin" && methodName == "remove") throw .      
    }
    //日志管理
    protected void AfterInvoke(String methodName)
    {

    }


    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================



    
    //导入学生数据到数据库中
    public string Import_Student_Info_From_Excel()
    {
        string result = "";

        try
        {
            HttpPostedFile file1 = HttpContext.Current.Request.Files["fu_UploadFile"];//如果是从JS调用，就用这个方法。参数名称与前台文件上传控件名称要一致。

            if (file1 == null)
            {
                throw new Exception("请先选择文件！");
            }
            else
            {

                string fileName = file1.FileName;/*获取文件名： C:\Documents and Settings\Administrator\桌面\123.jpg*/
                string suffix = fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower();/*获取后缀名并转为小写： jpg*/
                
                int bytes = file1.ContentLength;//获取文件的字节大小  

                if (!(suffix == "xls"))
                    throw new Exception("只能上传Excel文件");
                if (bytes > 1024 * 1024 * 10)
                    throw new Exception("文件最大只能传10M");
                
                if (bytes>0)
                {
                    byte[] byts = new byte[file1.ContentLength];

                    file1.InputStream.Read(byts, 0, byts.Length);
                    file1.InputStream.Close();

                    MemoryStream ms = new MemoryStream(byts);

                    //以下方法中的插入语句字段名称个数及顺序，要与Excel文件的标题对应起来（名称、个数、顺序）。
                    //存储过程中的语句insert familyinfo(sname,sex,rxnf,cid,fname,mobile,isteacher)
                    int i = ExcelRender.RenderToDb(ms, "Exec Add_New_Family  ", SqlHelper.ExecuteNonQuery);

                    if (i > 0)
                    {
                        result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "导入学生信息成功，共计导入：" + i.ToString().Trim() + "条！").ToJson();
                    }
                    else
                    {
                        result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "未导入此Excel表！").ToJson();
                    }
                }
                else
                {
                    result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "导入学生信息 失败！").ToJson();
                }
                
            }
        
        }
        catch (System.Exception ex)
        {
            result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "导入学生信息时发生严重错误，请联系管理员解决！"+ex.Message).ToJson();
        }

        return result;
    }


       
}